﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
using System.IO;

namespace card
{
    public partial class Form1 : Form
    {

        public static WebDG.Service ws = new card.WebDG.Service();
        string role = "";
        string cardadd = "";
        string io_port = "";

        public Form1()
        {
            InitializeComponent();
        }

        //public static OracleDatabase db =
        //   new OracleDatabase("Data Source=sjzx;User ID=dbcenter;Password=ok;Unicode=True;Load Balance Timeout=1000");

        private void btn_login_Click(object sender, EventArgs e)
        {


            cardadd = comboBox1.Text;
            //cardadd = "东门";
            io_port = comboBox3.Text;
            //io_port = "COM1";
            role = comboBox2.Text;
            //role = "全部";
            Session.Role = comboBox2.Text;

            string str_temp1 = "";
            //string str_temp2="";

            if (text_user.Text.Length == 0)
            {
                MessageBox.Show("请输入用户名。");
                text_user.Focus();
            }
            else
            {
                string strYHBH = text_user.Text.Trim().ToUpper();
                string sqlStr = "select KLKL,XM from user_info where YHBH='" + strYHBH + "'";
                DataSet ds = ws.getDataSet_Serv(sqlStr);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    str_temp1 = ds.Tables[0].Rows[0][0].ToString().ToUpper();

                    //Get the shippers
                    string sql_shippers = "select SHIPPER_NAME,SHIPPER_BRIEF,SHIPPER_BRIEF||'/'||SHIPPER_SHOT as DESCRIPTION from V_SHIPPER_DOC_TCC";
                    DataSet ds_shippers = ws.getDataSet_Serv(sql_shippers);
                    Session.Shippers = ds_shippers;
                    //Set the Operator
                    Session.Operator_User = ds.Tables[0].Rows[0][1].ToString();
                    //Get the cargo_names
                    string sql_cargo_names = "select CARGO_KIND_NO,CARGO_NAME_COD,CARGO_NAM,CARGO_NAM||'/'||CARGO_NAME_COD as DISPLAYNAME from V_SHIP_CARGO_NAME_TCC";
                    DataSet ds_cargo_name = ws.getDataSet_Serv(sql_cargo_names);
                    Session.Cargo_names = ds_cargo_name;

                    Session.CanEditLXH = CanEditLXH(text_user.Text.ToUpper());
                    Session.YHBH = strYHBH;
                    //Get the ship names dataset
                    //string sql_ship_names = "select SHIPNO||'/'||CSHIPNAME||'/'||ICARGONAME||'/'||ICAPACITY||'/'||VOYAGE||'/'||IFTRADE as DISPLAYNAME,VOYAGE,CSHIPNAME from v_ship_tcc";
                    //DataSet ds_ship_name = ws.getDataSet_Serv(sql_ship_names);
                    //Session.Ship_Name = ds_ship_name;
                }

                if (str_temp1 == text_pwd.Text.ToUpper())
                {
                    //MessageBox.Show("ok");
                    this.Hide();
                    FrmMenu frm1 = new FrmMenu();
                    frm1.Text = "选择操作类型-" + Session.Operator_User.ToString();
                    frm1.role = role;
                    frm1.cardadd = cardadd;
                    frm1.io_port = io_port;
                    frm1.Show();
                }
                else
                {
                    MessageBox.Show("用户名或者密码不正确，请重新输入。");
                    text_user.Focus();
                }
            }
        }

        private bool CanEditLXH(string sYHBH)
        {
            bool canEdit = false;
            DataSet ds = ws.getDataSet_Serv("select YHBH from S_RYGL_SENDCARD where YHBH = '" + sYHBH + "' and QX = '3'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                canEdit = true;
            }
            return canEdit;
        }

        private void btn_change_Click(object sender, EventArgs e)
        {
            Frm_PWDchange frm1 = new Frm_PWDchange();
            frm1.Text = text_user.Text.ToUpper();
            frm1.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //this.Size.Height = 260;

            int i = 0;
            comboBox1.Items.Clear();
            string str_sql = "";
            str_sql = "select name from sys_code where tablename='truck_login' and col='cardadd'";
            //DataTable dt2 = db.ExecuteDataSet(db.GetSqlStringCommand(str_sql.ToString())).Tables[0];
            DataSet ds = ws.getDataSet_Serv(str_sql);
            //listBox1.DataSource = ds;
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    comboBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
                }
            }
            comboBox1.SelectedIndex = 0;

            comboBox2.Items.Clear();
            str_sql = "select name from sys_code where tablename='truck_login' and col='ioyardid'";
            //DataTable dt2 = db.ExecuteDataSet(db.GetSqlStringCommand(str_sql.ToString())).Tables[0];
            DataSet ds2 = ws.getDataSet_Serv(str_sql);
            //listBox1.DataSource = ds;
            if (ds2.Tables[0].Rows.Count > 0)
            {
                for (i = 0; i < ds2.Tables[0].Rows.Count; i++)
                {
                    comboBox2.Items.Add(ds2.Tables[0].Rows[i][0].ToString());
                }
            }
            comboBox2.SelectedIndex = 0;

            comboBox3.Items.Clear();
            str_sql = "select name from sys_code where tablename='truck_login' and col='port'";
            //DataTable dt2 = db.ExecuteDataSet(db.GetSqlStringCommand(str_sql.ToString())).Tables[0];
            DataSet ds3 = ws.getDataSet_Serv(str_sql);
            //listBox1.DataSource = ds;
            if (ds3.Tables[0].Rows.Count > 0)
            {
                for (i = 0; i < ds3.Tables[0].Rows.Count; i++)
                {
                    comboBox3.Items.Add(ds3.Tables[0].Rows[i][0].ToString());
                }
            }
            comboBox3.SelectedIndex = 0;

        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void text_user_TextChanged(object sender, EventArgs e)
        {

        }

        private void text_pwd_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

    }
}
